package com.peng.leetcode.greedy;

import java.util.*;

/**
 * MinSubsequence
 * <p>
 * 1403. 非递增顺序的最小子序列
 *
 * @author: lupeng6
 * @create: 2021/2/1 11:33
 */
public class MinSubsequence {

    public static void main(String[] args) {
        int[] nums = {4, 3, 10, 9, 8};
        System.out.println(new MinSubsequence().minSubsequence(nums));
    }

    public List<Integer> minSubsequence(int[] nums) {
        int sum = 0;
        int subSum = 0;
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);

        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            sum += num;
            maxHeap.offer(num);
        }

        List<Integer> ans = new ArrayList<>(nums.length);

        while (sum >= subSum) {
            Integer poll = maxHeap.poll();
            sum -= poll;
            subSum += poll;
            ans.add(poll);
        }
        return ans;
    }
}
